// index.js
Page({
  data: {
    userAvatar: 'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',
    hasWordsToReview: false, // 初始不显示红点
    username: '微信用户',
    weekDay: 1,
    todayWordCount: 0,
    todayStudyMin: 0,
    openId: '',
    weekStudyCount: 0,
    weekTarget: 0,
    continuousDays: 0,
    weekTarget: 0,
    progressPercent: 0,
    currentwordbook: ''
  },
  onLoad() {
    // 页面加载逻辑
    // 获取当前日期是本周的第几天
    const date = new Date();
    const weekDay = date.getDay(); // 0 表示周日，1 表示周一，以此类推
    this.setData({
      weekDay: weekDay
    })
    // 获取今日学习单词数量
    this.getTodayWordCount();
    // 获取今日学习时长
    this.getTodayStudyMin();
    // 获取到微信用户信息
    this.getUserInfo();
    // 获取本周掌握单词数量
    this.getWeekStudyCount();
    // 获取连续学习天数
    this.getContinuousDays();
  },
  // 获取用户信息的方法
  async getUserInfo() {
    try {
      const result = await wx.cloud.callFunction({
        name: 'userFunctions', 
        data: {
          type: 'getUserInfo'
        }
      })
      
      if (result.result.success) {
        const userInfo = result.result.data
        console.log('用户信息:', userInfo)
        
        // 更新页面数据
        this.setData({
          userAvatar: userInfo.avatar,
          username: userInfo.nickname,
          weekTarget: userInfo.weekTarget,
          currentwordbook: userInfo.currentwordbook
        })
        // 计算学习进度
        this.calculateProgress();

      } else {
        console.error('获取用户信息失败:', result.result.message)
        wx.showToast({
          title: '点击头像可设置个人信息',
          icon: 'none'
        })
      }
    } catch (error) {
      console.error('调用云函数失败:', error)
      wx.showToast({
        title: '网络错误',
        icon: 'none'
      })
    }
  },
  // 计算学习进度
  calculateProgress() {
    const { weekStudyCount, weekTarget } = this.data;
    let progressPercent = 0;
    if (weekTarget > 0) {
      progressPercent = Math.round((weekStudyCount / weekTarget) * 100);
    }
    this.setData({
      progressPercent
    })
  },
  handleStudyWords() {
    if (this.data.currentwordbook == '') {
      wx.showToast({
        title: '请先选择单词本',
        icon: 'none'
      })
      return
    }
    // 学习单词
    wx.navigateTo({
        url: '/pages/studywords/index'
    })
  },
  handleReviewWords() {
    // 复习单词
    wx.navigateTo({
        url: '/pages/reviewwords/index'
    })
  },
  handleMemoryBook() {
    // 进入记忆本
    wx.navigateTo({
        url: '/pages/memory/index'
    })
  },
  getTodayWordCount() {
    // 从本地存储获取今日学习单词数量
    const todayWordCount = wx.getStorageSync('todayWordCount');
    this.setData({
      todayWordCount: todayWordCount || 0
    })
  },
  updateTodayWordCount(todayWordCount) {
    this.setData({
      todayWordCount: todayWordCount
    })
    // 取出weekStudyCount
    let weekStudyCount = wx.getStorageSync('weekStudyCount');
    weekStudyCount += todayWordCount;
    this.setData({
      weekStudyCount: weekStudyCount
    })
    // 计算学习进度
    this.calculateProgress();
  },
  getTodayStudyMin() {
    // 从本地存储获取今日学习时长
    const todayStudyMin = wx.getStorageSync('todayStudyMin');
    this.setData({
      todayStudyMin: todayStudyMin || 0
    })
  },
  setUserInfo() {
    // 跳转到用户设置
    wx.navigateTo({
      url: '/pages/setuserinfo/index'
    })
  },
  onShow() {
    // 页面显示时刷新数据
    this.getTodayWordCount();
    this.getTodayStudyMin();
  },
  // 获取本周掌握单词数量
  getWeekStudyCount() {
    // 从云函数获取本周掌握单词数量
    wx.cloud.callFunction({
      name: 'userFunctions',
      data: {
        type: 'getCurWeekStudyCount'
      },
      success: (res) => {
        // console.log(res)
        if (res.result.success) {
          let weekStudyCount = res.result.count
          // 保存weekStudyCount到本地
          wx.setStorageSync('weekStudyCount', weekStudyCount);
          weekStudyCount += (this.data.todayWordCount || 0);
          this.setData({
            weekStudyCount: weekStudyCount
          })
          // 计算学习进度
          this.calculateProgress();
        }
      }
    })
  },
  getContinuousDays() {
    // 从本地存储获取连续学习天数
    const continuousDays = wx.getStorageSync('continuousDays');
    this.setData({
      continuousDays: continuousDays || 0
    })
  }
})